/*
 * BPlusTreeLeafNode.h
 *
 *  Created on: 30-Oct-2013
 *      Author: sriram
 */

#ifndef BPLUSTREELEAFNODE_H_
#define BPLUSTREELEAFNODE_H_
#include "../Global/StructureHeaders.h"
#include "IndexHeaderPage.h"
class BPlusTreeLeafNode {
private:
	typedef struct LeafNodeStruct{
		PageHeader pgHeader;				// General Page Header
		int leftPGNumber, rightPGNumber; // Left and right pages to the node
		int level, noRecords, parentPGNumber, indexHeaderPGNumber;
	}LeafNodeHeaderStructure;

	LeafNodeHeaderStructure leafNodePageHeader_;	// Leaf NOde header
	IndexHeaderPage *indexHeaderPage_;				// Metadata of Index
	char *pageData_;

	bool isHeaderCreated;

public:
	BPlusTreeLeafNode();
	BPlusTreeLeafNode(int leafPGNumber);
	BPlusTreeLeafNode(IndexHeaderPage *indexHeaderPage,int leafPGNumber);
	virtual ~BPlusTreeLeafNode();
	STATUS_CODE create(IndexHeaderPage *indexHeaderPage);// Creating a page
        STATUS_CODE insert(char *key, RIDStruct &rid);
        int search(char *key, RIDStruct rid);
        int contains(char *key, int operatorType);
        int contains(char *key);
        int containsColumns(char *key, int noColumns, int operatorType);
        int containsColumns(char *key, vector<int> operatorType);
        int containsColumns(char *key, int noColumns);
        
        
        STATUS_CODE storeRidsOfKey(char *key, vector<RIDStruct> &arrRid);
        STATUS_CODE storeRidsOfKey(char *key, int operatorType, vector<RIDStruct> &arrRid);
        STATUS_CODE storeRidsOfKey(char *key, vector<int> arrOperatorType, vector<RIDStruct> &arrRid);
        STATUS_CODE remove(char *key, RIDStruct &rid, vector<int> &deletedPages);
        
        // Needed when trying to insert in the first or middle of a incomplete
        void getFirstKey(char *key);
        void getFirstKey(char *key, RIDStruct &rid);
        void getLastKey(char *key, RIDStruct &rid);
        void getParentPrevKey(char *prevKey);
        
        
        int getPageNumber();    
	void getKey(int keyNumber, char *key);
	void getKey_Rid(int keyNumber, char *key, RIDStruct &rid);
	static int getLeafNodeHeaderSize();
	int getIndexHeaderPgNumber();
	void setIndexHeaderPgNumber(int indexHeaderPgNumber);
	int getLeftPgNumber();
	void setLeftPgNumber(int leftPgNumber);
	int getLevel();
	void setLevel(int level);
	int getNoRecords();
	void setNoRecords(int noRecords);
	int getParentPgNumber();
	void setParentPgNumber(int parentPgNumber);
	int getRightPgNumber();
	void setRightPgNumber(int rightPgNumber);
        string toString();
        string toShortString();
        
        // Checks for consistency
        bool checkConsistency();
};

#endif /* BPLUSTREELEAFNODE_H_ */
/*
 * TODO:
 * 1. Complete the function to get ParentPrevKey (From the BPlusTreeNode dependent)
 * 2. Complete the function to replace key in parent Node(BPlusTreeNode)
 *    and uncomment the parentNode.replaceKey(); 
 * 
 * Deletion:
 * 
 */